// JavaScript Document
// 需要引入userCheckPublic.js文件


$(document).ready(function() {	
	/* 提交添加银行卡表单 */
	$('#btn-bankadd').click(function() {		
		if(checksuccess()){
			$('#widthdraw').submit();
		}
	});

	/* 提交提款表单 */
	$('#btn-platformpay').click(function() {
		if(fee()){
			$('#widthdraw').submit();
		}		
	});
		
});


function amountError(){
	$(".input-prepend").attr("class", "input-prepend clearfix error");
	$("#prependedInput").focus();
}
function amountSuccess(){
	$(".input-prepend").attr("class", "input-prepend clearfix success");
}

function checkfee() {
	var bkid = $("input[name='bkid']:checked").val() // 选中的银行卡
	if (bkid != null) {
		if (fee()) {
			return true;
		} else {
			return false;
		}
	} else {
		alert('选择银行卡');
		return false;
	}
}

function fee() {
	var currcash = $.trim($("#currcash").text()); // 可用余额
	var amount = $("#prependedInput").val(); // 提取金额
	var fee = $("#hand-money"); // 手续费
	var real = $("#real-money"); // 实际到账

	currcash=parseFloat(currcash);
	//amount=parseFloat(amount);
	
	var amountTips = $("#amountTips"); // 提取金额提示
	var isWithdraw = /^[0-9]+([.]{1}[0-9]{1,2})?$/;// 验证是否符合以纯数字为起点，小数后两位
	
	if (amount == null || amount == '') {
		$("#amountTips").text("请输入提款金额！");
		amountError();
		return false;
	} else if (isNaN(amount)) {
		$("#amountTips").text("请输入正确的提款金额！");
		amountError();
		return false;
	} else if (!isWithdraw.test(amount)) {
		$("#amountTips").text("提现金额最多可含2位小数!");
		amountError();
		return false;
	} else {		
		if (currcash < 50) {
			amountTips.text("可用余额不足 50 元！");
			amountError();
			return false;
		}else if(amount>currcash){
			amountTips.text("提现金额不能超过 " +currcash+" 元！");
			amountError();
			return false;
		} else {			
			if (amount < 50) {	
				amountTips.text("提现金额需大于 50 元小于等于 100,000 元！");
				amountError();
				return false;
			} else if (amount <= 300) {				
				amountTips.text("");
				fee.text(1.5);
				real.text(amount - 1.5);
				amountSuccess();
				return true;
			} else if (amount <= 500) {
				amountTips.text("");
				fee.text(3);
				real.text(amount - 3);
				amountSuccess();
				return true;
			} else if (amount <= 5000) {
				amountTips.text("");
				fee.text(5);
				real.text(amount - 5);
				amountSuccess();
				return true;
			} else if (amount <= 100000) {
				amountTips.text("");
				fee.text(10);
				real.text(amount - 10);
				amountSuccess();
				return true;
			} else {				
				amountTips.text("提现金额需大于 50 元小于等于100000 元！");
				amountError();
				return false;
			}
		}
	}

}

// 删除地址
function delBank(bankid,url) {
	/* 提交表单 */
	$(document).ready(function() {
		var choice = confirm("您确认要删除这个张银行卡吗？", function() { }, null);
		if (choice == true)// 如果确定的话
			$.post(url, {bankid : bankid},
			function(data) {// ajax开始		
				 location.reload() ;
			}, 'json');// ajax结束

	});
}

/* 检查所有输入框是否达到要求 */
function checksuccess(n) {
	var realname = $("#realname").val(); 
	var realnameTips= $("#realnameTips");
	
	if(realname=="" || realname==null){
		showTips(realnameTips, '请输入真实姓名！', null);
		return false;
	}
	
	var resideprovince = $("#resideprovince"); // 省份
	var residecity = $("#residecity"); // 城市
	var residedist = $("#residedist"); // 地区
	var bankname = $("#bankname"); // 银行名
	var bankid = $("#bankid"); // 银行卡卡号
	
	var bankidTips = $("#bankidTips"); // 收件人tips

	switch (n) {
	case 'bank': // 单项验证收件人
		checkInput(bankid, bankidTips, '', '', '', 'bank');
		return false;
		break;
	case 'select': // 单项验证收件人
		if (checkInput(resideprovince, '', '', '', '', 'select'))
			if (checkInput(residecity, '', '', '', '', 'select'))
				if (checkInput(residedist, '', '', '', '', 'select'))
					if (checkInput(bankname, '', '', '', '', 'select'))
						return false;
		break;
	default: // 检查所有的输入项
		if (checkInput(resideprovince, '', '', '', '', 'select')
				&& checkInput(residecity, '', '', '', '', 'select')
				&& checkInput(residedist, '', '', '', '', 'select')
				&& checkInput(bankname, '', '', '', '', 'select')
				&& checkInput(bankid, bankidTips, '', '', '', 'bank')) {
			return true;
		}
	}

	return false;
}

